//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
   public class RelativeRectangle : IOfficeXmlSerializableAs 
    {
        private double? b;
        private double? l;
        private double? r;

        private double? t;

        public double? Left
        {
            get { return l; }
            set { l = value; }
        }

        public double? Top
        {
            get { return t; }
            set { t = value; }
        }

        public double? Right
        {
            get { return r; }
            set { r = value; }
        }

        public double? Bottom
        {
            get { return b; }
            set { b = value; }
        }

        #region IOfficeXmlSerializableAs Members

        void IOfficeXmlSerializableAs.ReadXmlAs(string name, ReadContext context)
        {
            using (var c = context.Read(name))
            {
                Left = c.GetOptionalInteger("l");
                if (Left != null)
                    Left = Left/factor;
                Top = c.GetOptionalInteger("t");
                if (Top != null)
                    Top = Top/factor;
                Right = c.GetOptionalInteger("r");
                if (Right != null)
                    Right = Right/factor;
                Bottom = c.GetOptionalInteger("b");
                if (Bottom != null)
                    Bottom = Bottom/factor;
            }
        }

       private const int factor = 100000;

        void IOfficeXmlSerializableAs.WriteXmlAs(string name, WriteContext context)
        {
            using (WriteContext c = context.Write(name))
            {
                if (Left != null) c.SetInteger("l", (int)(Left.Value * factor));
                if (Top != null) c.SetInteger("t", (int)(Top.Value * factor));
                if (Right != null) c.SetInteger("r", (int)(Right.Value * factor));
                if (Bottom != null) c.SetInteger("b", (int)(Bottom.Value * factor));
            }
        }

        #endregion

        internal Rect ToRect()
        {
            return new Rect(Left.Value, Top.Value, Right.Value - Left.Value, Bottom.Value - Top.Value);
        }
    }
}